
class SilentPostBacksController < ApplicationController
  # http://www.mydomain.com/silent_post_back
  def create
    # Don't process postbacks that aren't for arb subscriptions
     return render(:text => 'hi') if params[:x_subscription_id].nil?

     case params[:x_response_code].to_i
       when ActiveMerchant::Billing::AuthorizeNetGateway::APPROVED
         handleApprovedPayment(params)
       when ActiveMerchant::Billing::AuthorizeNetGateway::DECLINED
         handleDeclinedPayment(params)
       when ActiveMerchant::Billing::AuthorizeNetGateway::ERROR
         handlePaymentError(params)
       when ActiveMerchant::Billing::AuthorizeNetGateway::FRAUD_REVIEW
         handleFraudReviewPayment(params)
     end
  end
  
  private
  
  def handleApprovedPayment(params)
    save_arb_report(params, "Payment Approved")
  end
  
  def handleDeclinedPayment(params)
    save_arb_report(params, "Payment Declined")
    puts "Send email to customer and notify them to update the credit card."
  end
  
  def handlePaymentError(params)
    save_arb_report(params, "Payment Error")
  end
  
  def handleFraudReviewPayment(params)
    save_arb_report(params, "Fraud Review Required")
    puts "Send email to admin for fraud review"
  end
  
  def save_arb_report(params, status)
    ArbStatus.create(:x_subscription_paynum => params[:x_subscription_paynum], 
                     :x_subscription_id => params[:x_subscription_id],
                     :status => status)
  end
end


